﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Avatar.Model;
using AvatarHelper;

namespace Avatar.ResumeService
{
    public static class EducationManager
    {
        public static void Save(string _user,
            Guid _id,
            string _school,
            int _diploma,
            DateTime _beginDate,
            DateTime _overDate,
            string _description,
            int _disciplines,
            bool _studyAbroad
            )
        {
            try
            {
                #region 入口检测
                if (string.IsNullOrEmpty(_user)) throw new Exception("用户名不能为空");
                if (string.IsNullOrEmpty(_school)) throw new Exception("学校名称不能为空");
                if (string.IsNullOrEmpty(_description)) throw new Exception("在校情况描述不能为空");
                if (_school.Length > 30) throw new Exception("学校名称不能超过30个字符");
                if (!string.IsNullOrEmpty(_description) && _school.Length > 500) throw new Exception("在校情况描述不能超过500字");
                #endregion

                using (var _adc = DBHelper.GetDataContext())
                {
                    var _resume = _adc.Resume.Where(r => r.User == _user).FirstOrDefault();
                    if (_resume != null)
                    {
                        var _e = _resume.Education.Where(e => e.ID == _id).FirstOrDefault();
                        if (_e == null)
                        {
                            if (_resume.Education.Count() > 10) throw new Exception("教育背景记录每人最多可发10条");
                            _e = new Education();
                            _e.ID = Guid.NewGuid();
                            _e.BeginDate = _beginDate;
                            _e.OverDate = _overDate;
                            _e.Description = _description;
                            _e.DiplomaID = _diploma;
                            _e.DisciplinesID = _disciplines;
                            _e.School = _school;
                            _e.StudyAbroad = _studyAbroad;
                            _e.User = _user;

                            _adc.Education.InsertOnSubmit(_e);
                        }
                        else
                        {
                            _e.BeginDate = _beginDate;
                            _e.OverDate = _overDate;
                            _e.Description = _description;
                            _e.DiplomaID = _diploma;
                            _e.DisciplinesID = _disciplines;
                            _e.School = _school;
                            _e.StudyAbroad = _studyAbroad;
                        }

                        _resume.UpdateDate = DateTime.Now;
                        _adc.SubmitChanges();
                    }
                }
            }
            catch (Exception _e)
            {
                throw new Exception("保存教育背景资料出错:"+_e.Message);
            }
        }
        public static List<Education> GetEducation(string _userName)
        {
            if (string.IsNullOrEmpty(_userName)) throw new Exception("用户名不能为空！");

            return DBHelper.GetAll<Education>(e => e.User == _userName, "获取用户教育背景失败：");
        }

        public static Education GetEducation(Guid _id)
        {
            return DBHelper.Get<Education>(e => e.ID == _id, "获取用户教育背景失败:");
        }

        public static void Delete(Guid _id)
        {
            try
            {
                using (var _adc = DBHelper.GetDataContext())
                {
                    var _e = _adc.Education.Where(e => e.ID == _id).FirstOrDefault();
                    if (_e == null) throw new Exception("所选记录不存在，请刷新后再试。");
                    _adc.Education.DeleteOnSubmit(_e);
                    _adc.SubmitChanges();
                }
            }
            catch (Exception _e)
            {
                throw new Exception("删除教育背景记录出错：" + _e.Message);
            }
        }
    }
}
